<template>
  <div class="ke-free-course">
    <div class="title">
      <ke-title :title="'免费课程'" />
    </div>
    <div class="content">
      <div class="left">
        <video-player
          class="video-player vjs-custom-skin"
          ref="videoPlayer"
          :playsinline="true"
          :options="playerOptions"
          @ready="beforPlay"
        />
      </div>
      <div class="right">
        <div
          class="item"
          v-for="(item, index) in courseNameList"
          :key="index"
          @click="changeCourse(index)"
        >{{item}}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ke-free-course',
  components: {
    keTitle: () => import('../ke-title')
  },
  data() {
    return {
      playerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
        autoplay: false, //如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [
          {
            type: 'video/mp4',
            src: 'http://localhost:9090/movies/freeCourse/1-2 准备工作.mp4'
          }
        ],
        poster: require('../../assets/images/login-banner.jpg'), //你的封面地址
        // width: document.documentElement.clientWidth, //播放器宽度
        notSupportedMessage: '此视频暂无法播放，请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true //全屏按钮
        }
      },
      freeCourse: {},
      courseNameList: []
    }
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player
    }
  },
  created() {
    this.getFreeCourseInfo()
  },
  methods: {
    getFreeCourseInfo() {
      this.$axios
        .get('/api/course/courseList', {
          params: {
            type: '免费课程'
          }
        })
        .then(res => {
          const course = res.data.data.ret[0].courseSrc
          this.freeCourse = course
          for (let i = 0; i < course.src.length; i++) {
            this.courseNameList.push(
              course.src[i].split('/')[course.src[i].split('/').length - 1]
            )
          }
        })
    },
    changeCourse(index) {
      this.playerOptions.sources[0].src = this.freeCourse.src[index]
    },
    beforPlay() {
      const isLogin = localStorage.ke_token
      if (!isLogin) {
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.ke-free-course {
  margin: 48px auto 55px;
  width: 1200px;
  overflow: hidden;

  .content {
    display: flex;
    flex-direction: row;
    height: 350px;

    .left {
      position: relative;
      flex: 1;
      text-align: center;

      /deep/.video-player {
        width: 100%;
        height: 100%;
        display: block;

        .video-js {
          height: 100%;
        }

        .vjs-poster {
          background-size: 100% 100%;
        }
      }

      /deep/.vjs-big-play-button {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: 50%;
        transform: translatex(-50%);
      }
    }

    .right {
      flex: 1;
      background-color: #eeeeee;
      box-sizing: border-box;
      border-top: 1px solid #eeeeee;
      border-bottom: 1px solid #eeeeee;
      overflow: scroll;
      -ms-overflow-style: none;

      &::-webkit-scrollbar {
        width: 0px;
      }

      .item {
        height: 50px;
        line-height: 50px;
        box-sizing: border-box;
        padding: 0 48px;
        cursor: pointer;

        &:hover {
          background: #ffffff;
          color: rgb(35, 184, 255);
        }
      }
    }
  }
}
</style>
